# -*- coding: utf-8 -*-
# @Time : 2023/12/25 12:38
# @Author : huangqiusen
# @Email : huangqiusen@zhiyitech.cn
import os
import cv2
import time
import json
import random
import base64
import requests
from PIL import Image
from io import BytesIO
from loguru import logger
from Crypto.Cipher import AES
from Crypto.Hash import SHA512

from dotenv import load_dotenv

load_dotenv()

H5_SDK_VERSION = os.getenv("H5_SDK_VERSION") or '3.5.18'
SALT = os.getenv(
    "SALT") or '36bad93d454fd4542d71d2566ab92c645aa500cda184e2e0ad8367adac20c209ddc77450110c9d66ba9abae3142e06134907a7c49c836e590d8d3709bd1edcd4'


def get_random_detail(fp):
    headers = {
        "authority": "fanqienovel.com",
        "accept": "application/json, text/javascript",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "content-type": "application/x-www-form-urlencoded",
        "origin": "https://fanqienovel.com",
        "pragma": "no-cache",
        "referer": "https://fanqienovel.com/main/writer/login",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
        "x-secsdk-csrf-token": "000100000001b7e57516eb615618fc1d1a2db17d175d1181e1a3993be1599744ebbdc47c613317a98c268e657d51",
        "x-tt-passport-csrf-token": ""
    }
    cookies = {
        "csrf_session_id": "0047aad17dcbf505e3d02d7b230a9115",
        "ttwid": "1%7CvVjB9g-K18vU0-N5tasWgX0cn-8qOa4WE0Xc1PxheJM%7C1705048031%7C2d7b96a54e369164b08ece6822fd7e364f240fc5dd4f12dbe83aece00f9dabca",
        "s_v_web_id": fp
    }
    url = "https://fanqienovel.com/passport/web/send_code/"
    params = {
        "aid": "2503",
        "account_sdk_source": "web",
        "sdk_version": "2.2.6-beta.2",
        "verifyFp": fp,
        "fp": fp
    }
    data = {
        "mix_mode": "1",
        "mobile": "2e3d3325343037373737303d313032",
        "type": "3731",
        "fixed_mix_mode": "1"
    }
    response = requests.post(url, headers=headers, cookies=cookies, params=params, data=data)
    data_dict = response.json()
    verify_center_decision_conf = json.loads(data_dict['data']['verify_center_decision_conf'])
    # logger.info(verify_center_decision_conf)
    fp = verify_center_decision_conf['fp']
    detail = verify_center_decision_conf['detail']
    return {
        'fp': fp,
        'detail': detail,
    }


def captcha_encrypt(data: dict) -> str:
    """
    AES GCM
    """

    def aes_gcm_encrypt(key, nonce, plaintext):
        cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
        plaintext = cipher.encrypt_and_digest(plaintext)
        return plaintext

    # 加密字节
    v8 = json.dumps(data, separators=(',', ':')).encode()
    # sha521加密 + 加密字节
    v11 = SHA512.new(v8).digest() + v8
    # 32位的盐
    slat = ''.join(random.choices("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", k=32))

    v12 = SHA512.new(bytes.fromhex(SHA512.new(slat.encode()).hexdigest() + SALT)).hexdigest()

    ciphertext, mac = aes_gcm_encrypt(key=bytes.fromhex(v12[:64]), nonce=bytes.fromhex(v12[64: 88]), plaintext=v11)
    return base64.b64encode(bytes([116, 99, 6, 16, 0, 0]) + slat.encode() + ciphertext + mac).decode()


def captcha_decrypt(enc_data: str) -> str:
    def aes_gcm_decrypt(key, nonce, ciphertext, tag):
        cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
        plaintext = cipher.decrypt_and_verify(ciphertext, tag)
        return plaintext

    data_bytes = base64.b64decode(enc_data.encode())
    hedaers_bytes = data_bytes[:6]
    # 秘文标志b'tc\x06\x10\x00\x00'
    if hedaers_bytes != bytes([116, 99, 6, 16, 0, 0]):
        raise Exception("Invalid header")
    slat_bytes = data_bytes[6: 38]  # 32
    cipher_bytes = data_bytes[38: -16]  # 38到后16位

    magic_bytes = data_bytes[-16:]  # 后16位
    # SALT  两把盐{密文base64字节流 + 页面js固定} => key-iv
    key_iv = SHA512.new(bytes.fromhex(
        SHA512.new(slat_bytes).hexdigest() +
        SALT
    )).hexdigest()
    key = key_iv[:64]
    iv = key_iv[64: 88]
    return json.loads(aes_gcm_decrypt(bytes.fromhex(key), bytes.fromhex(iv), cipher_bytes, magic_bytes)[64:].decode())


def get_my_distance(repair_path, target_path, bg_path):
    """
    opencv来完成计算 cv2
    pip install python-opencv
    pip install opencv-python
    :return:
    """
    # 读取两张图
    bg = cv2.imread(repair_path)
    slice = cv2.imread(target_path)
    # 删除该图片
    os.remove(repair_path)
    os.remove(target_path)
    os.remove(bg_path)

    # 做灰度处理
    bg = cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY)
    slice = cv2.cvtColor(slice, cv2.COLOR_BGR2GRAY)

    # 图片边缘处理
    bg_can = cv2.Canny(bg, 255, 255)
    slice = cv2.Canny(slice, 255, 255)

    # 匹配图像的相似度, TM_CCOEFF_NORMED参数固定即可
    r = cv2.matchTemplate(bg_can, slice, cv2.TM_CCOEFF_NORMED)

    # 获取匹配度最好的一个结果
    minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(r)
    x = maxLoc[0]
    # logger.info("识别距离: %s" % (x,))
    return int(x * 0.6159420289855072)


# def get_distance(bk_path, target_path):
#     """
#     ddddocr 识别滑动距离
#     """
#     import ddddocr
#     det = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)
#     with open(bk_path, 'rb') as f1, open(target_path, 'rb') as f2:
#         return det.slide_match(f1.read(), f2.read())["target"][0]

def get_distance(target_bytes, background_bytes):
    """
    ddddocr 识别滑动距离
    """
    import ddddocr
    det = ddddocr.DdddOcr(det=False, ocr=False,show_ad=False)  # , show_ad=False
    return int(det.slide_match(target_bytes, background_bytes)["target"][0] * 0.6159420289855072)


def save_file(file_path: str, bs: bytes):
    # "./repair.jpg", repair_bytes
    # 写入数据到文件中
    with open(file_path, mode='wb') as f:
        f.write(bs)
    # logger.info("{} 图片保存成功！".format(file_path))


def get_fp():
    e = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    t = len(e)
    milliseconds = int(round(time.time() * 1000))
    base36 = ''
    while milliseconds > 0:
        remainder = milliseconds % 36
        if remainder < 10:
            base36 = str(remainder) + base36
        else:
            base36 = chr(ord('a') + remainder - 10) + base36
        milliseconds = int(milliseconds / 36)
    r = base36
    o = [''] * 36
    o[8] = o[13] = o[18] = o[23] = '_'
    o[14] = '4'

    for i in range(36):
        if not o[i]:
            n = 0 or int(random.random() * t)
            if i == 19:
                n = 3 & n | 8
            o[i] = e[n]
    ret = "verify_" + r + "_" + ''.join(o)
    return ret


def get_tracks(distance, _y):
    """
    获取轨迹参数
    """
    tracks = list()
    y, v, t, current = 0, 0, 1, 0
    mid = distance * 3 / 4
    exceed = random.randint(40, 90)
    z = random.randint(30, 150)
    while current < (distance + exceed):
        if current < mid / 2:
            a = 2
        elif current < mid:
            a = 3
        else:
            a = -3
        a /= 2
        v0 = v
        s = v0 * t + 0.5 * a * (t * t)
        current += int(s)
        v = v0 + a * t
        y += random.randint(-3, 3)
        z = z + random.randint(5, 10)
        tracks.append([min(current, (distance + exceed)), y, z])
    while exceed > 0:
        exceed -= random.randint(0, 5)
        y += random.randint(-3, 3)
        z = z + random.randint(5, 9)
        tracks.append([min(current, (distance + exceed)), y, z])
    tr = []
    for i, x in enumerate(tracks):
        tr.append({
            'x': x[0],
            'y': _y,
            'relative_time': x[2]
        })
    return tr


def get_current_timestamp13():
    """
    获取13位数时间戳
    :return:
    """
    return "{}".format(int(time.time() * 1e3))


def repair_image(bg_resp_content: bytes) -> bytes:
    """
    图片还原
    """
    # 加载原始滑块验证码图片
    image_bytes_io = BytesIO(bg_resp_content)
    # 使用 Image.open() 打开类文件对象
    original_image = Image.open(image_bytes_io)
    # 计算每个小块的长度
    width, height = original_image.size
    block_width = width // 6
    # 分割图片为6个小块
    slices = []
    for i in range(6):
        left = i * block_width
        right = (i + 1) * block_width
        slices.append(original_image.crop((left, 0, right, height)))

    # 正确还原顺序
    slices[2], slices[4] = slices[4], slices[2]
    slices[3], slices[5] = slices[5], slices[3]
    slices[0], slices[2] = slices[2], slices[0]
    slices[1], slices[5] = slices[5], slices[1]
    slices[1], slices[2] = slices[2], slices[1]
    # 创建一个新的图片对象，将处理后的小块图片合并为一张图片
    new_image = Image.new('RGB', (width, height))
    for i in range(6):
        new_image.paste(slices[i], (i * block_width, 0))
    # 将图片保存到内存中
    image_bytes_io = BytesIO()
    new_image.save(image_bytes_io, format='JPEG')
    # 获得图片的字节数据
    return image_bytes_io.getvalue()


def image_restore(bg_path):
    """
    图片还原 保存图片
    """
    # 加载原始滑块验证码图片
    original_image = Image.open(bg_path)
    # 计算每个小块的长度
    width, height = original_image.size
    block_width = width // 6
    # 分割图片为6个小块
    slices = []
    for i in range(6):
        left = i * block_width
        right = (i + 1) * block_width
        slices.append(original_image.crop((left, 0, right, height)))
    # 正确还原顺序
    slices[2], slices[4] = slices[4], slices[2]
    slices[3], slices[5] = slices[5], slices[3]
    slices[0], slices[2] = slices[2], slices[0]
    slices[1], slices[5] = slices[5], slices[1]
    slices[1], slices[2] = slices[2], slices[1]
    # 创建一个新的图片对象，将处理后的小块图片合并为一张图片
    new_image = Image.new('RGB', (width, height))
    for i in range(6):
        new_image.paste(slices[i], (i * block_width, 0))
    # 保存处理后的图片
    repair_path = "./repair.jpg"
    new_image.save(repair_path)
    return repair_path


class DouYinSlide():

    def __init__(self):

        self.headers = {
            "Accept": "application/json, text/plain, */*",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Content-Type": "application/x-www-form-urlencoded",
            "Origin": "https://haohuo.jinritemai.com",
            "Pragma": "no-cache",
            "Referer": "https://haohuo.jinritemai.com/ecommerce/trade/detail/index.html?id=3634653387691285185&ins_activity_param=id5J6q7J&origin_type=pc_buyin_group&pick_source=v.bw0o7",
        }

    def get_shop_detail(self, promotion_ids):
        """抖音商品详情"""
        cookies = {
            "sessionid": os.getenv("SESSIONID") or 'e926b3ade98e099c4abd4fa2ab1fceb7',  # 暂时写死
        }

        params = {
            "is_h5": "1",
            "is_native_h5": "1",
            "verifyFp": get_fp(),
            "origin_type": "pc_buyin_group",
            "msToken": "",
            "a_bogus": "",
        }
        data = {
            "ui_params": "{\"from_live\":false,\"from_video\":null,\"three_d_log_data\":null,\"follow_status\":null,\"which_account\":null,\"ad_log_extra\":null,\"from_group_id\":null,\"bolt_param\":null,\"transition_tracker_data\":null,\"selected_ids\":null,\"window_reposition\":null,\"is_short_screen\":null,\"full_mode\":true}",
            "use_new_price": "1",
            "is_h5": "1",
            "bff_type": "2",
            "is_in_app": "0",
            "promotion_ids": promotion_ids,
            "meta_param": "",
            "source_page": "",
            "request_additions": "",
            "isFromVideo": "false",
            "enable_timing": "true"
        }
        resp = requests.post("https://ecom5-normal-hl.ecombdapi.com/aweme/v2/shop/promotion/pack/h5/",
                             headers=self.headers,
                             cookies=cookies,
                             params=params,
                             data=data)

        return resp.json()

    def get_tracks(self, distance, tip_y):
        """
        获取轨迹参数
        """
        tracks = list()
        y, v, t, current = 0, 0, 1, 0
        mid = distance * 3 / 4
        exceed = random.randint(40, 90)
        z = random.randint(30, 150)
        while current < (distance + exceed):
            if current < mid / 2:
                a = 2
            elif current < mid:
                a = 3
            else:
                a = -3
            a /= 2
            v0 = v
            s = v0 * t + 0.5 * a * (t * t)
            current += int(s)
            v = v0 + a * t
            y += random.randint(-3, 3)
            z = z + random.randint(5, 10)
            tracks.append([min(current, (distance + exceed)), y, z])
        while exceed > 0:
            exceed -= random.randint(0, 5)
            y += random.randint(-3, 3)
            z = z + random.randint(5, 9)
            tracks.append([min(current, (distance + exceed)), y, z])
        tr = []
        for i, x in enumerate(tracks):
            tr.append({
                'x': x[0],
                'y': tip_y,
                'relative_time': x[2]
            })
        return tr

    def pass_slide(self, detail, fp=None):
        if not fp:
            fp = get_fp()
        logger.info('X音滑块'.center(50, '-'))
        logger.info('fp: %s || detail: %s' % (fp, detail))
        session = requests.session()
        session.headers.update({
            "accept": "*/*",
            "accept-language": "zh-CN,zh;q=0.9",
            "cache-control": "no-cache",
            "origin": "https://rmc.bytedance.com",
            "pragma": "no-cache",
            "priority": "u=1, i",
            "referer": "https://rmc.bytedance.com/",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
        })
        params = {
            "aid": "7886",
            "lang": "zh",
            "repoId": "579047",
            "subtype": "slide",
            "detail": detail,
            "server_sdk_env": "\\{\"idc\":\"lf\",\"region\":\"CN\",\"server_type\":\"passport\"\\}",
            "mode": "slide",
            "fp": fp,
            "h5_check_version": "4.0.5",
            "os_name": "windows",
            "platform": "pc",
            "os_type": "2",
            "h5_sdk_version": H5_SDK_VERSION,
            "webdriver": "false",
            "tmp": get_current_timestamp13()
        }
        # 1.获取图片 参数等等
        response = session.get("https://verify.zijieapi.com/captcha/get", params=params)
        js_data = response.json()
        code = js_data['code']
        if code != 200:
            raise Exception("获取滑块/数据响应错误！")

        id = js_data['data']['id']
        bk_url = js_data['data']['question']['url1']
        target_url = js_data['data']['question']['url2']
        tip_y = js_data['data']['question']['tip_y']

        bg_bytes = session.get(bk_url).content
        target_bytes = session.get(target_url).content

        # 图片处理方案一:  图片存储+还原
        # bg_path, target_path = "./bg.jpg", "./target.jpg"
        # save_file(bg_path, bg_bytes)
        # save_file(target_path, target_bytes)
        # distance = get_my_distance(repair_bytes, target_path, bg_path)  # css 比例缩

        # 图片处理方案二:  字节流
        # repair_bytes = image_restore(bg_bytes)
        # distance = get_distance(target_bytes, repair_bytes)  # css 比例缩

        distance = get_distance(target_bytes, bg_bytes)  # css 比例缩
        logger.info(f'识别距离: {distance}')
        # 滑动轨迹
        tracks = get_tracks(distance, tip_y)

        encrypt_data = {'modified_img_width': 340,
                        'id':id,
                        'mode': 'slide',
                        'ej9xmy6': tracks,
                        'mmNCFqKt': [],
                        'xbM80h': {'H299xC': {'x': 83, 'y': 326, 'time': 1730914211604},
                                   'HFLa': {'x': 81, 'y': 318, 'time': 1730914211605},
                                   'NjKjLvO8': [{'x': 54, 'y': 359, 'time': 1730914130269},
                                                {'x': 60, 'y': 370, 'time': 1730914130304},
                                                {'x': 0, 'y': 214, 'time': 1730914134536},
                                                {'x': 92, 'y': 164, 'time': 1730914134578},
                                                {'x': 260, 'y': 92, 'time': 1730914208960},
                                                {'x': 185, 'y': 168, 'time': 1730914209009},
                                                {'x': 106, 'y': 248, 'time': 1730914209046},
                                                {'x': 73, 'y': 291, 'time': 1730914210610},
                                                {'x': 113, 'y': 277, 'time': 1730914210644},
                                                {'x': 128, 'y': 271, 'time': 1730914210678},
                                                {'x': 139, 'y': 264, 'time': 1730914210713},
                                                {'x': 145, 'y': 259, 'time': 1730914210751},
                                                {'x': 144, 'y': 259, 'time': 1730914210794},
                                                {'x': 119, 'y': 273, 'time': 1730914210856},
                                                {'x': 102, 'y': 282, 'time': 1730914210890},
                                                {'x': 84, 'y': 288, 'time': 1730914210925},
                                                {'x': 84, 'y': 290, 'time': 1730914211105},
                                                {'x': 84, 'y': 331, 'time': 1730914211584},
                                                {'x': 80, 'y': 313, 'time': 1730914211620},
                                                {'x': 75, 'y': 303, 'time': 1730914211655},
                                                {'x': 72, 'y': 300, 'time': 1730914211801},
                                                {'x': 71, 'y': 300, 'time': 1730914211842},
                                                {'x': 71, 'y': 300, 'time': 1730914211950},
                                                {'x': 82, 'y': 300, 'time': 1730914211987},
                                                {'x': 91, 'y': 300, 'time': 1730914212024},
                                                {'x': 103, 'y': 300, 'time': 1730914212062},
                                                {'x': 115, 'y': 299, 'time': 1730914212096},
                                                {'x': 125, 'y': 298, 'time': 1730914212132},
                                                {'x': 134, 'y': 296, 'time': 1730914212166},
                                                {'x': 145, 'y': 296, 'time': 1730914212201},
                                                {'x': 151, 'y': 295, 'time': 1730914212238},
                                                {'x': 157, 'y': 295, 'time': 1730914212281}], 'Tl0eACfJZ': [],
                                   'lg7Fkjdy': [{'x': 71, 'y': 300, 'time': 1730914211923, 't': 0},
                                                {'x': 71, 'y': 300, 'time': 1730914211950, 't': 0},
                                                {'x': 73, 'y': 300, 'time': 1730914211962, 't': 0},
                                                {'x': 75, 'y': 300, 'time': 1730914211963, 't': 0},
                                                {'x': 78, 'y': 300, 'time': 1730914211975, 't': 0},
                                                {'x': 80, 'y': 300, 'time': 1730914211980, 't': 0},
                                                {'x': 82, 'y': 300, 'time': 1730914211987, 't': 0},
                                                {'x': 84, 'y': 300, 'time': 1730914211997, 't': 0},
                                                {'x': 87, 'y': 300, 'time': 1730914212005, 't': 0},
                                                {'x': 89, 'y': 300, 'time': 1730914212018, 't': 0},
                                                {'x': 91, 'y': 300, 'time': 1730914212025, 't': 0},
                                                {'x': 93, 'y': 300, 'time': 1730914212032, 't': 0},
                                                {'x': 96, 'y': 300, 'time': 1730914212037, 't': 0},
                                                {'x': 98, 'y': 300, 'time': 1730914212046, 't': 0},
                                                {'x': 100, 'y': 300, 'time': 1730914212055, 't': 0},
                                                {'x': 103, 'y': 300, 'time': 1730914212062, 't': 0},
                                                {'x': 105, 'y': 300, 'time': 1730914212068, 't': 0},
                                                {'x': 107, 'y': 300, 'time': 1730914212073, 't': 0},
                                                {'x': 109, 'y': 300, 'time': 1730914212080, 't': 0},
                                                {'x': 112, 'y': 300, 'time': 1730914212086, 't': 0},
                                                {'x': 113, 'y': 299, 'time': 1730914212088, 't': 0},
                                                {'x': 115, 'y': 299, 'time': 1730914212096, 't': 0},
                                                {'x': 117, 'y': 299, 'time': 1730914212103, 't': 0},
                                                {'x': 120, 'y': 299, 'time': 1730914212111, 't': 0},
                                                {'x': 122, 'y': 299, 'time': 1730914212120, 't': 0},
                                                {'x': 123, 'y': 298, 'time': 1730914212123, 't': 0},
                                                {'x': 125, 'y': 298, 'time': 1730914212132, 't': 0},
                                                {'x': 128, 'y': 298, 'time': 1730914212140, 't': 0},
                                                {'x': 130, 'y': 298, 'time': 1730914212146, 't': 0},
                                                {'x': 132, 'y': 298, 'time': 1730914212155, 't': 0},
                                                {'x': 134, 'y': 298, 'time': 1730914212162, 't': 0},
                                                {'x': 136, 'y': 296, 'time': 1730914212168, 't': 0},
                                                {'x': 138, 'y': 296, 'time': 1730914212176, 't': 0},
                                                {'x': 140, 'y': 296, 'time': 1730914212186, 't': 0},
                                                {'x': 142, 'y': 296, 'time': 1730914212192, 't': 0},
                                                {'x': 145, 'y': 296, 'time': 1730914212201, 't': 0},
                                                {'x': 147, 'y': 296, 'time': 1730914212216, 't': 0},
                                                {'x': 148, 'y': 295, 'time': 1730914212222, 't': 0},
                                                {'x': 150, 'y': 295, 'time': 1730914212233, 't': 0},
                                                {'x': 153, 'y': 295, 'time': 1730914212243, 't': 0},
                                                {'x': 155, 'y': 295, 'time': 1730914212255, 't': 0},
                                                {'x': 157, 'y': 295, 'time': 1730914212281, 't': 0}], 'aSm0x': []},
                        'env': {'canvas_hash': '6cc1c0d0a0548ec6199de8c968aaedf1',
                                'webgl_hash': '62e11136104bd996d8ada7b925e51d65',
                                'font_hash': '65820f817f980fd9ef25b65ffc78aeb4a279f7702604',
                                'audio_hash': 124.04347527516074, 'time_offset': -480, 'time_zone': 'Asia/Shanghai',
                                'languages': ['zh-CN'],
                                'plugins': ['PDF Viewer', 'Chrome PDF Viewer', 'Chromium PDF Viewer',
                                            'Microsoft Edge PDF Viewer', 'WebKit built-in PDF'], 'platform': 'Win32',
                                'max_touch_points': 10, 'webdriver': False, 'touch_actions': [],
                                'mouse_actions': ['1,1', '1,1', '1,1', '1,1'], 'device': {},
                                'os': {'name': 'Windows', 'version': '10'},
                                'browser': {'name': 'Chrome', 'version': '127.0.0.0'},
                                'engine': {'name': 'Blink', 'version': '127.0.0.0'},
                                'gpu': {'vendor': 'Google Inc. (Intel)',
                                        'renderer': 'ANGLE (Intel, Intel(R) UHD Graphics 630 (0x00003E9B) Direct3D11 vs_5_0 ps_5_0, D3D11)'},
                                'fps': 45, 'resolution': '1920,1080', 'browser_size': '1585,809',
                                'page_size': '1569,680', 'captcha_origin': '0,0', 'captcha_size': '380, 384',
                                'mask_time': 173091158866443, 'loading_time': 1730911589345,
                                'ready_time': 1730911589719}, 'a': 54}
        # 核心加密逻辑
        captchaBody = captcha_encrypt(encrypt_data)

        logger.info(f'captchaBody: {captchaBody} {type(captchaBody)}')
        params = {
            "aid": "7886",
            "lang": "zh",
            "repoId": "579047",
            "subtype": "slide",
            "detail": detail,
            "server_sdk_env": "{\"idc\":\"lf\",\"region\":\"CN\",\"server_type\":\"passport\"}",
            "mode": "slide",
            "fp": fp,
            "h5_check_version": "4.0.5",
            "os_name": "windows",
            "platform": "pc",
            "os_type": "2",
            "h5_sdk_version": H5_SDK_VERSION,
            "webdriver": "false",
            "tmp": get_current_timestamp13(),
            "xx-tt-dd": "qJI7ttpVdGKKbSBvYqmaf0aPo",
        }

        time.sleep(random.uniform(2, 3))
        data = json.dumps({
                                    "captchaBody": captchaBody
                                }, separators=(',', ':'))
        # 2.滑块校验
        response = session.post("https://verify.zijieapi.com/captcha/verify",
                                params=params,
                                data=data,
                                )

        data_dict = response.json()
        logger.info(f'接口数据: {data_dict}')
        data_dict = response.json()
        return {
            'fp': fp,
            'detail': detail,
            'message': data_dict['message']
        }




dy_helper = DouYinSlide()


